/*

*/

(function($) { 
$.fn.touchzone = function(settings) {
	var config = {
		disabled: false,
		cancel: ':input,option',
		containment: false,
		handle: false,
		event_callback: null,
		preventDefaultEvents: true
	};
	// ##################################################
	function touchstart(t){

	}
	function touchmove(t){	

	}
	function touchend(t){

	}	
	// ##################################################
	function checkTouches(all_touches){
		// loop thru touches to see what ones are no longer active
		var toRemove = [];
		for (var i = 0; i < touches.length; i++){
			var found = false;
			for (var j = 0; j < all_touches.length; j++ ){
				if (touches[i].identifier == all_touches[j].identifier){
					found = true;
				}
			}
			if (!found){
				toRemove.push({identifier:touches[i].identifier});
			}
		}	
		// remove inactive touches
		for (var i = 0; i < toRemove.length; i++){
			j = touchIndex(toRemove[i].identifier);
			if (j > -1){
				var t = touches[j];
				touches.splice(j ,1);
				touchend(t);
			}
		}
	}
	function touchIndex(identifier){
		for (var j = 0 ; j < touches.length; j++){
			if (touches[j].identifier == identifier){
				return j;
			}
		}
		return -1;
	}
	function getTickCount(){
		var now = new Date();
	    return now.getTime();
	}
	function getTouch(identifier){
		for (var j = 0 ; j < touches.length; j++){
			if (touches[j].identifier == identifier){
				return touches[j];
			}
		}
		return null;
	}	
	// #####################################  Main  #####################################	
	if (settings) $.extend(config, settings);
	var selector = this.selector;
	if (selector == '') return false;	
	var touches = [];
	//$('body').delegate(selector,'touchstart', function() {
	$(selector).bind('touchstart', function() {
		event.preventDefault();
		var allTouches = event.touches;
		for (var i = 0; i < allTouches.length; i++){
			if (touchIndex(allTouches[i].identifier) == -1){	
				var touch = allTouches[i];		
				var starttick = getTickCount();
				var t = {identifier:touch.identifier,
						pageX:touch.pageX,
						pageY:touch.pageY,
						startPageX:touch.pageX,
						startPageY:touch.pageY,
						starttick:starttick};
				touches.push(t);
				// touchstart
				touchstart(t);				
			}
		}
	});
	//$('body').delegate(selector,'touchend', function() {
	$(selector).bind('touchend', function() {
		event.preventDefault();
		checkTouches(event.touches);
	});
	//$('body').delegate(selector,'touchmove', function() {
	$(selector).bind('touchmove', function() {
		event.preventDefault();
		var allTouches = event.touches;
		for (var i = 0; i < allTouches.length; i++){
			var touch = allTouches[i];
			var j = touchIndex(touch.identifier);
			// is it in our touches array?
			if (j > -1){
				// has it moved?
				if (touches[j].pageX != touch.pageX || touches[j].pageY != touch.pageY){
					// this touch has moved
					touches[j].pageX = touch.pageX;
					touches[j].pageY = touch.pageY;
					touchmove(touches[j]);
				}
			}
		}
	});
	return this;
};
})(jQuery);